package com.huaweicloud.apm.example;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonProperty;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

public class ListApps {

    private static final String IAM_HEADER_TOEKN = "X-Auth-Token";

    private static final String IAM_TOKEN_VALUE = "{用户获取的token}";

    private static final String X_BUSINESS_ID = "x-business-id";

    private static final String OPEN_API_URL_LIST_APPS = "https://{endPoint}/v1/apm2/openapi/cmdb/apps/get-apps";

    public static GetAppListVO getAppListVO(Long businessId, Long xBusinessId) {
        HttpHeaders headers = new HttpHeaders();
        headers.add(IAM_HEADER_TOEKN, IAM_TOKEN_VALUE);
        headers.add(X_BUSINESS_ID, String.valueOf(xBusinessId));

        RestTemplate restTemplate = Utils.restTemplate();
        ResponseEntity<String> response = restTemplate.exchange(OPEN_API_URL_LIST_APPS + "?business_id=" + businessId,
            HttpMethod.GET, new HttpEntity<String>(headers), String.class, new HashMap<>());
        String body = response.getBody();
        JSONObject jsonObject = JSON.parseObject(body, JSONObject.class);
        GetAppListVO vo = new GetAppListVO();
        List<AppNodeModel> nodeModels = new ArrayList<>();
        JSONArray jsonArray = jsonObject.getJSONArray("apps");
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject json = jsonArray.getJSONObject(i);
            AppNodeModel appNodeModel = new AppNodeModel();
            appNodeModel.setBusinessId(json.getLong("business_id"));
            appNodeModel.setId(json.getLong("id"));
            appNodeModel.setSubBusinessId(json.getLong("sub_business_id"));
            appNodeModel.setGmtCreate(json.getDate("gmt_create"));
            appNodeModel.setInnerDomainId(json.getInteger("inner_domain_id"));
            appNodeModel.setGmtModify(json.getDate("gmt_modify"));
            appNodeModel.setName(json.getString("name"));
            nodeModels.add(appNodeModel);
        }
        vo.setApps(nodeModels);
        return vo;
    }

    static class GetAppListVO {
        @JsonProperty("apps")
        private List<AppNodeModel> apps;

        public GetAppListVO() {
        }

        public GetAppListVO(List<AppNodeModel> apps) {
            this.apps = apps;
        }

        public List<AppNodeModel> getApps() {
            return apps;
        }

        public void setApps(List<AppNodeModel> apps) {
            this.apps = apps;
        }
    }

    static class AppNodeModel implements Serializable {
        private static final long serialVersionUID = 1L;

        @JsonProperty("sub_business_id")
        private Long subBusinessId;

        @JsonProperty("gmt_create")
        private Date gmtCreate;

        @JsonProperty("name")
        private String name;

        @JsonProperty("gmt_modify")
        private Date gmtModify;

        @JsonProperty("business_id")
        private Long businessId;

        @JsonProperty("id")
        private Long id;

        @JsonProperty("inner_domain_id")
        private Integer innerDomainId;

        public Long getId() {
            return id;
        }

        public void setBusinessId(Long businessId) {
            this.businessId = businessId;
        }

        public Long getSubBusinessId() {
            return subBusinessId;
        }

        public void setGmtCreate(Date gmtCreate) {
            this.gmtCreate = gmtCreate;
        }

        public void setSubBusinessId(Long subBusinessId) {
            this.subBusinessId = subBusinessId;
        }

        public Integer getInnerDomainId() {
            return innerDomainId;
        }

        public Date getGmtModify() {
            return gmtModify;
        }

        public void setGmtModify(Date gmtModify) {
            this.gmtModify = gmtModify;
        }

        public String getName() {
            return name;
        }

        public void setId(Long id) {
            this.id = id;
        }

        public Date getGmtCreate() {
            return gmtCreate;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Long getBusinessId() {
            return businessId;
        }

        public void setInnerDomainId(Integer innerDomainId) {
            this.innerDomainId = innerDomainId;
        }
    }

}
